﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Wfxin.Core
{
    /// <summary>
    /// 时间戳
    /// </summary>
    public class TimeHelp
    {
        #region 时间戳[10|13]转为C#格式时间  
        /// <summary>
        ///  时间戳[10|13]转为C#格式时间       
        /// </summary>
        public static DateTime StampToDateTime(string stamp)
        {
            if (stamp.Length != 10 || stamp.Length != 13) return DateTime.Now;
            try
            {
                DateTime StartDateTime = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1), TimeZoneInfo.FindSystemTimeZoneById("China Standard Time"));
                if (stamp.Length == 10) StartDateTime.AddSeconds(long.Parse(stamp));
                return StartDateTime.AddMilliseconds(long.Parse(stamp));
            }
            catch (Exception)
            {
                return DateTime.Now;
            }
        }
        #endregion

        #region 获取时间戳
        /// <summary>
        /// 获取时间戳
        /// </summary>
        /// <returns></returns>
        public static string DateTimeToStamp(System.DateTime time, int length = 13)
        {
            long ts = ConvertDateTimeTolong(time);
            return ts.ToString().Substring(0, length);
        }
        #endregion

        #region DateTime时间格式转换为Unix时间戳格式
        /// <summary>
        /// DateTime时间格式转换为Unix时间戳格式
        /// </summary>
        private static long ConvertDateTimeTolong(DateTime datetime)
        {

            try
            {
                //return datetime.Ticks - TimeZoneInfo.ConvertTime(new DateTime(1970, 01, 01), TimeZoneInfo.FindSystemTimeZoneById("China Standard Time")).Ticks;
                //return Convert.ToInt64((datetime - new DateTime(1970, 01, 01, 00, 00, 00, 0000)).TotalMilliseconds);
                return (datetime.ToUniversalTime().Ticks - 621355968000000000) / 10000;
            }
            catch (Exception)
            {
                return 0;
            }
        }
        #endregion



    }
}
